package com.young.weixin.model;

import com.young.common.util.StringUtils;

/**
 * 微信access_token存储对象
 *
 * @author wangjiyu
 * @date 2021/10/5 13:34
 */
public class AccessTokenHolder {
    /**
     * 微信access_token
     */
    private String accessToken;
    /**
     * 过期时间 毫秒数
     */
    private long expireTimes;

    /**
     * 构造器
     * @param accessToken access_token
     * @param seconds 有效时间,单位:秒
     */
    private AccessTokenHolder(String accessToken, int seconds) {
        super();
        if (StringUtils.isBlank(accessToken)) {
            throw new RuntimeException("accessToken is blank");
        }
        this.accessToken = accessToken;
        this.expireTimes = System.currentTimeMillis() + (seconds * 1000);
    }

    /**
     * 构造
     * @param accessToken access_token
     * @param seconds 有效时间,单位:秒
     * @return
     */
    public static AccessTokenHolder build(String accessToken, int seconds) {
        return new AccessTokenHolder(accessToken, seconds);
    }

    /**
     * 判断是否有效
     * @return true表示token有效
     */
    public boolean isValid() {
        return System.currentTimeMillis() < this.expireTimes;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public long getExpireTimes() {
        return expireTimes;
    }

    public void setExpireTimes(long expireTimes) {
        this.expireTimes = expireTimes;
    }
}
